package com.teacold.customdemo.chapter_one;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.util.AttributeSet;
import android.view.View;

public class RegionDemo extends View {
    private Paint mPaint;

    public RegionDemo(Context context) {
        super(context);
        init();
    }

    public RegionDemo(Context context, AttributeSet attrs) {
        this(context);
    }

    public RegionDemo(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.RED);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        regionOp(canvas);

    }

    /**
     * Region Op方法
     */
    private void regionOp(Canvas canvas){
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.DKGRAY);
     Rect rectF=new Rect(10,50,130,90);
     Rect rectF2=new Rect(50,10,90,130);
     canvas.drawRect(rectF,mPaint);
     canvas.drawRect(rectF2,mPaint);
     Region region=new Region(rectF);
     Region region1=new Region(rectF2);
     region.op(region1,Region.Op.INTERSECT);

     mPaint.setColor(Color.BLUE);
     mPaint.setStyle(Paint.Style.FILL);
     drawRegion(canvas,region);
    }
    /**
     * Region umion方法
     */
    private void regionUnion(Canvas canvas) {
        Region region=new Region(60,10,200,100);
        region.union(new Rect(10,10,50,300));
        drawRegion(canvas,region);
    }
    /**
     * Region方法
     */
    private void regionOval(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        Path ovalPath = new Path();
        RectF rectF = new RectF(50, 50, 200, 500);
        ovalPath.addOval(rectF, Path.Direction.CCW);
        //构造椭圆区域
        Region region = new Region();
        region.setPath(ovalPath, new Region(50, 50, 200, 500));
        drawRegion(canvas, region);
    }
    /**
     * Region的setPath方法
     */
    private void region(Canvas canvas) {
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        Path ovalPath = new Path();
        RectF rectF = new RectF(50, 50, 200, 500);
        ovalPath.addOval(rectF, Path.Direction.CCW);
        Region region = new Region();
        region.setPath(ovalPath, new Region(50, 50, 200, 200));
        //画出路径
        drawRegion(canvas, region);
    }

    /**
     * Region简单使用
     */
    private void drawRegion(Canvas canvas, Region region) {
//        Region region = new Region(new Rect(50, 50, 200, 100));
        RegionIterator regionIterator = new RegionIterator(region);
        Rect rect = new Rect();
        while (regionIterator.next(rect)) {
            canvas.drawRect(rect, mPaint);
        }
    }

}
